﻿namespace NJQuery {
	using System;
	using System.ComponentModel;
	using System.Web.UI;

	/// <summary>
	/// Specifies source/destination element size/state for an effect.
	/// </summary>
	public class EffectSize : IClientHash {


		#region Fields

		StateBag viewState;

		#endregion


		#region Constructors

		/// <summary>
		/// Initializes a new instance of the <see cref="EffectSize" /> class.
		/// </summary>
		/// <param name="viewState">The container control's ViewState object.</param>
		/// <exception cref="ArgumentNullException">View State cannot be null.</exception>
		public EffectSize(StateBag viewState) {
			if(viewState == null) throw new ArgumentNullException("viewState");

			this.viewState = viewState;
		}

		#endregion


		#region Properties: Public

		/// <summary>
		/// Gets or sets the element height.
		/// </summary>
		[DefaultValue(typeof(int?), "")]
		public virtual int? Height {
			get { return viewState["Height"] as int?; }
			set { viewState["Height"] = value; }
		}

		/// <summary>
		/// Gets or sets the element width.
		/// </summary>
		[DefaultValue(typeof(int?), "")]
		public virtual int? Width {
			get { return viewState["Width"] as int?; }
			set { viewState["Width"] = value; }
		}

		/// <summary>
		/// Gets whether or not the defined properties are all set to default values.
		/// </summary>
		public bool IsDefault {
			get { return Height == null && Width == null; }
		}

		#endregion


		#region Properties: Protected

		/// <summary>
		/// Gets a dictionary of state information that allows you to save and restore the view state of a server control across multiple requests for the same page.
		/// </summary>
		protected StateBag ViewState {
			get { return viewState; }
		}

		#endregion


		#region Methods: Rendering hooks

		/// <summary>
		/// Creates the properties object that will be rendered as the jQuery UI initialization options.
		/// This method is used primarily by control developers.
		/// </summary>
		/// <param name="properties">A <see cref="PropertyCollection"/> object that will become the jQuery UI initialization options.</param>
		protected virtual void AddInitProperties(PropertyCollection properties) {

			int? height = Height;
			if(height.HasValue) properties.AddValue("height", height.Value);

			int? width = Width;
			if(width.HasValue) properties.AddValue("width", width.Value);

		}

		/// <summary>
		/// Returns a literal JavaScript value used to represent the server value on the client.
		/// </summary>
		/// <param name="indent">The client source indentation level.</param>
		/// <returns>The client script value.</returns>
		public string ToClientSource(int indent) {
			PropertyCollection childProperties = new PropertyCollection(indent);
			AddInitProperties(childProperties);
			return childProperties.ToClientSource();
		}

		#endregion


	}

}